import {_decorator, Component, Label, Node, Sprite, SpriteFrame, tween, v3, Vec3} from 'cc';
import {GameMode, StripColors} from "./GameConfig";

const { ccclass, property } = _decorator;

@ccclass('LevelConditionBall')
export class LevelConditionBall extends Component {

    public ballSprite:Sprite=null;
    public numLabel:Label=null;
    public checkSpriteNode:Node=null;

    public lineGameNode:Node=null;
    public lineSprite:Sprite=null;
    public ball0Sprite:Sprite=null;
    public ball1Sprite:Sprite=null;

    public mode:GameMode=GameMode.BallGame;
    //小球对是否被连接,用于LineGame
    public connected:boolean=false;

    public colorId:number=0;
    public num:number=0;

    //记录配置上的num,用于reset
    public cfg_num:number=0;

    private inited:boolean=false;

    public static readonly ballDefaultScale:Vec3=v3(0.65, 0.65, 1);
    public static readonly checkSpriteScale:Vec3=v3(0.5, 0.5, 1);

    onLoad()
    {
        this.init();
    }

    public init()
    {
        if(this.inited)
            return;

        this.ballSprite=this.node.getChildByName("ball").getChildByName("ballSprite").getComponent(Sprite);
        this.numLabel=this.node.getChildByName("num").getComponent(Label);
        this.checkSpriteNode=this.node.getChildByName("checkSprite");
        this.checkSpriteNode.active=false;

        this.lineGameNode=this.node.getChildByName("lineGame");
        this.lineSprite=this.lineGameNode.getChildByName("line").getComponent(Sprite);
        this.ball0Sprite=this.lineGameNode.getChildByName("ball0").getChildByName("ballSprite").getComponent(Sprite);
        this.ball1Sprite=this.lineGameNode.getChildByName("ball1").getChildByName("ballSprite").getComponent(Sprite);

        this.inited=true;
    }

    public setBallMode(sf:SpriteFrame, num:number, colorId:number)
    {
        this.init();

        this.ballSprite.node.parent.active=true;
        this.numLabel.node.active=true;
        this.lineGameNode.active=false;

        this.ballSprite.spriteFrame=sf;
        this.numLabel.string=""+num;
        this.colorId=colorId;

        this.cfg_num=num;
        this.num=num;
        this.mode=GameMode.BallGame;
    }

    public setLineGameMode(sf:SpriteFrame, colorId:number)
    {
        this.init();
        this.ballSprite.node.parent.active=false;
        this.numLabel.node.active=false;
        this.lineGameNode.active=true;

        this.ball0Sprite.spriteFrame=sf;
        this.ball1Sprite.spriteFrame=sf;
        this.colorId=colorId;
        this.lineSprite.color=StripColors[colorId]
        this.mode=GameMode.LineGame;
    }

    //条件显示重置
    public reset()
    {
        this.num=this.cfg_num;
        this.numLabel.string=""+this.num;
        if(this.mode===GameMode.BallGame)
        {
            this.numLabel.node.active = true;
        }
        else if(this.mode===GameMode.LineGame)
        {
            this.numLabel.node.active=false;
        }
        this.checkSpriteNode.active=false;
    }

    public scaleAnim()
    {
        tween(this.ballSprite.node.parent).to(0.05, {scale:v3(1.05, 1.05, 1)}).to(0.05,{scale:LevelConditionBall.ballDefaultScale}).call(()=>{

        }).start();
    }

    public lineConnected(connected:boolean)
    {
        this.connected=connected;

        if(connected)
        {
            this.checkSpriteNode.active=true;
            this.checkSpriteNode.setScale(0.1, 0.1, 1);
            tween(this.checkSpriteNode).to(0.5, {scale:LevelConditionBall.checkSpriteScale},{easing:"backOut"}).call(()=>{

            }).start();
        }
        else
        {
            this.checkSpriteNode.active=false;
        }
    }

    public ballsCollected(count:number)
    {
        if(count<=0)
            return;
        this.num-=count;
        if(this.num<=0)
            this.num=0;

        this.scheduleOnce(()=>{
            this.numLabel.string=""+this.num;
            if(this.num===0)
            {
                this.numLabel.node.active=false;
                this.checkSpriteNode.active=true;
                this.checkSpriteNode.setScale(0.1, 0.1, 1);
                tween(this.checkSpriteNode).to(0.5, {scale:LevelConditionBall.checkSpriteScale},{easing:"backOut"}).call(()=>{

                }).start();
            }
        }, 0.8);
    }

    public isClear():boolean
    {
        if(this.num>0)
            return false;
        return true;
    }

}

